﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace My.Domain.Issues
{
    public class IssueManager : DomainService
    {
        private readonly IRepository<Issue, Guid> _issueRepository;

        public IssueManager(IRepository<Issue, Guid> issueRepository)
        {
            _issueRepository = issueRepository;
        }

        public async Task AssignToAsync(Issue issue, Guid userId)
        {
            //不允许用户有超过3个打开的问题
            var openIssueCount = await _issueRepository.CountAsync(i => i.AssignedUserId == userId && !i.IsClosed);

            if (openIssueCount >= 3)
            {
                throw new BusinessException(code: "123", message: "用户已经绑定超过3个已打开的问题");
            }

            issue.AssignedUserId = userId;
        }
    }
}
